﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Globalization;
using System.Text;
using System.Security.Cryptography;
using System.Configuration;
using System.Threading;

/// <summary>
/// Summary description for Helper
/// </summary>
public static class Helper
{
    public static decimal GetSellPrice(decimal costPrice, decimal actual, decimal percent)
    {
        decimal sellPrice = costPrice;
        decimal perP = costPrice * percent / 100;
        sellPrice += perP > actual ? perP : actual;
        return sellPrice;
    }
    public static int QSIntValue(string key)
    {
        int id = 0;

        if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString[key]))
            int.TryParse(HttpContext.Current.Request.QueryString[key], out id);

        return id;
    }
    public static bool QSBoolValue(string key)
    {
        bool id = false;

        if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString[key]) && HttpContext.Current.Request.QueryString[key] == "1")
            id = true;

        return id;
    }
    public static decimal QSDecimalValue(string key)
    {
        decimal id = 0;

        if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString[key]))
            decimal.TryParse(HttpContext.Current.Request.QueryString[key], out id);

        return id;
    }
    public static string QSValue(string key)
    {
        if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString[key]))
            return HttpContext.Current.Request.QueryString[key].ToString();

        return string.Empty;
    }
    public static DateTime QSDateValue(string key)
    {
        if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString[key]))
            return DateFromString(HttpContext.Current.Request.QueryString[key].ToString(), Constants.QS_DATE_FORMAT);

        return DateTime.MinValue;
    }
    public static void ApplyDeleteConfirmMsg(GridViewRowEventArgs e, int iDeleteCell)
    {
        ApplyDeleteConfirmMsg(((LinkButton)e.Row.Cells[iDeleteCell].Controls[0]));
    }
    public static void ApplyDeleteConfirmMsg(LinkButton link)
    {
        link.OnClientClick = "return confirm(\"Are you sure you want to delete?\");";
    }
    public static DateTime DateFromString(string strDate)
    {
        return DateFromString(strDate, Constants.DATE_FORMAT);
    }
    public static DateTime DateFromString(string strDate, string format)
    {
        DateTime dt = new DateTime();

        DateTime.TryParseExact(strDate, format, new CultureInfo("en-US"),
                              DateTimeStyles.None, out dt);

        return dt;
    }
    public static String ApplicationPath
    {
        get
        {
            String appPath = HttpContext.Current.Request.ApplicationPath;
            if (String.IsNullOrEmpty(appPath)) appPath = "/";
            return appPath;
        }
    }
    public static string GetRequestUrl(string excludeParams)
    {
        StringBuilder builder = new StringBuilder();
        string[] queParams = HttpContext.Current.Request.Url.Query.Replace("?", string.Empty).Split('&');
        foreach (string qp in queParams)
        {
            string[] qv = qp.Split('=');
            if (qv.Length == 2 && !string.IsNullOrEmpty(qv[0]) && !string.IsNullOrEmpty(qv[1]) && !excludeParams.Contains("|" + qv[0].ToLower() + "|"))
            {
                if (builder.Length == 0)
                    builder.Append("?");
                else
                    builder.Append("&");
                builder.Append(qv[0] + "=" + qv[1]);
            }
        }
        return builder.ToString();
    }

    public static string GetBookingReferenceNO(int bookingId)
    {
        return ConfigurationSettings.AppSettings["BookingRefPreFix"] + "-" 
        + bookingId.ToString("000000");
    }

    #region Query String Manipulations

    #endregion

    #region Encryption - Decryption
    public static string EncryptString(string Message, string Passphrase)
    {
        byte[] Results;
        System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
        MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
        byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase));
        TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
        TDESAlgorithm.Key = TDESKey;
        TDESAlgorithm.Mode = CipherMode.ECB;
        TDESAlgorithm.Padding = PaddingMode.PKCS7;
        byte[] DataToEncrypt = UTF8.GetBytes(Message);
        try
        {

            ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor();
            Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length);
        }
        finally
        {
            TDESAlgorithm.Clear();
            HashProvider.Clear();
        }

        return Convert.ToBase64String(Results);
    }
    public static string DecryptString(string Message, string Passphrase)
    {
        byte[] Results;
        System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
        MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
        byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase));
        TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
        TDESAlgorithm.Key = TDESKey;
        TDESAlgorithm.Mode = CipherMode.ECB;
        TDESAlgorithm.Padding = PaddingMode.PKCS7;
        byte[] DataToDecrypt = Convert.FromBase64String(Message);
        try
        {
            ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor();
            Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length);
        }

        finally
        {
            TDESAlgorithm.Clear();
            HashProvider.Clear();
        }
        return UTF8.GetString(Results);
    }
    #endregion
}
